﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Samples
{
    /// <summary>
    /// Defines a helper class to implement quick handling camera
    /// </summary>
    public class CameraHandler
    {
        #region variables
        private Camera camera = null;//camera to handle
        public float RotationSpeed { get; set; }//speed of rotation
        public float MoveSpeed { get; set; }//speed of move
        public Point Center; //center of screen
        private DemoFramework framework;
        private bool mouseDown = false;
        #endregion

        #region Constructor
        /// <summary>
        /// Create a cameraHandler
        /// </summary>
        /// <param name="camera">Camera to handle</param>
        /// <param name="rotationSpeed">Speed of rotation</param>
        /// <param name="moveSpeed">Speed of move</param>
        public CameraHandler(DemoFramework framework, Camera camera, float rotationSpeed, float moveSpeed)
        {
            this.framework = framework;
            this.camera = camera;
            this.MoveSpeed = moveSpeed;
            this.RotationSpeed = rotationSpeed;
            this.mouseDown = false;
            Center = new Point();
        }
        #endregion

        #region IUpdatable Members

        /// <summary>
        /// Update handler and at result camera
        /// </summary>
        /// <param name="gameTime">GameTime</param>
        /// <remarks>This method must be culled in update method of appliation</remarks>
        public void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            MouseState mouse = framework.Mouse;
            KeyboardState keyboard = framework.Keyboard;

            if (mouse.LeftButton == ButtonState.Pressed)
            {
                if (!mouseDown)
                {
                    Center.X = mouse.X;
                    Center.Y = mouse.Y;
                }
                else
                {
                    camera.Yaw += (Center.X - mouse.X) * RotationSpeed * gameTime.ElapsedGameTime.Milliseconds;
                    camera.Pitch += (Center.Y - mouse.Y) * RotationSpeed * gameTime.ElapsedGameTime.Milliseconds;
                    Center.X = mouse.X;
                    Center.Y = mouse.Y;
                }
                mouseDown = true;
            }
            else
            {
                mouseDown = false;
            }
            Vector3 v = Vector3.Zero;
            if (keyboard.IsKeyDown(Keys.W))
                v.Z += MoveSpeed * gameTime.ElapsedGameTime.Milliseconds;
            else if (keyboard.IsKeyDown(Keys.S))
                v.Z -= MoveSpeed * gameTime.ElapsedGameTime.Milliseconds;
            if (keyboard.IsKeyDown(Keys.A))
                v.X -= MoveSpeed * gameTime.ElapsedGameTime.Milliseconds;
            else if (keyboard.IsKeyDown(Keys.D))
                v.X += MoveSpeed * gameTime.ElapsedGameTime.Milliseconds;
            if (keyboard.IsKeyDown(Keys.Q))
                v.Y += MoveSpeed * gameTime.ElapsedGameTime.Milliseconds;
            if (keyboard.IsKeyDown(Keys.E))
                v.Y -= MoveSpeed * gameTime.ElapsedGameTime.Milliseconds;
            camera.Move(v.X, v.Y, v.Z);

        }

        #endregion
    }
}
